// ColladaLoadPlug.cpp : Defines the entry point for the DLL application.
//

#include "stdafx.h"
#include "xColladaFilePlug.h"






namespace XE
{



	//xColladaFilePlug::xColladaFilePlug(const xString& name)
	//	:xFilePlug(name)
	//{



	//}

	xColladaFilePlug::xColladaFilePlug()
		:xFilePlug(),m_pdae(NULL)
	{
		m_ExtendNameList.push_back("dae");

	}



	xColladaFilePlug::~xColladaFilePlug()
	{
		SafeDelete(m_pdae)
	}

	bool  xColladaFilePlug::SaveFile(const xString& filename)
	{
		return true;
	}

	bool xColladaFilePlug::LoadFile(const xString& name,xSceneManager* pSceneManager)
	{
		if(m_pdae==NULL)
			m_pdae = new DAE();

		daeInt re = m_pdae->load(name.c_str());
		if (re!= DAE_OK) 
		{
			xLogMessager::getSingleton().logMessage("open File faile name is: "+name);
			SafeDelete(m_pdae)
			return false;
		}
		

		domCOLLADA *dom = m_pdae->getDom(name.c_str());

	


		const  domLibrary_geometries_Array&   GeomeArray = dom->getLibrary_geometries_array();
		size_t GeomeArrayCount =   GeomeArray.getCount();
		for(size_t j = 0;j<GeomeArrayCount;++j)
		{
			domLibrary_geometriesRef pGeometryLib =   GeomeArray.get(j);
			LoadGeometrys(pGeometryLib);
		}

	




		for ( size_t i = 0; i < dom->getLibrary_images_array().getCount(); i++)
		{
		
			domLibrary_imagesRef  pDaeTex= dom->getLibrary_images_array().get(i);

			
			for ( size_t j= 0; j< pDaeTex->getImage_array().getCount(); j++)
			{
				
				domImageRef lib = pDaeTex->getImage_array().get(j);

				domImage* imageElement = (domImage*)(domElement*)lib;

				 xString fileaneme =  imageElement->getId();
				 
				 int cc = 0;
				 cc++;
			}



		}



        m_pdae->unload(name.c_str());
		return true;
	}




	bool xColladaFilePlug::LoadGeometrys(const  domLibrary_geometriesRef& pGeometrlib)
	{
		size_t GeomeCount = pGeometrlib->getGeometry_array().getCount();
		for(size_t m = 0;m<GeomeCount;++m)
		{
			domGeometryRef pGeometry = pGeometrlib->getGeometry_array().get(m);
			 LoadGeometry(pGeometry);
		}
		return true;
	}

	bool xColladaFilePlug::LoadGeometry(const  domGeometryRef& pGeometry)
	{
		domMeshRef pMesh = pGeometry->getMesh();
		if(pMesh==NULL)
			return false;

		size_t sourceCount=  pMesh->getSource_array().getCount();

		domList_of_floats& pvertex =  pMesh->getSource_array().get(0)->getFloat_array()->getValue();
		domList_of_floats& pnormal =  pMesh->getSource_array().get(1)->getFloat_array()->getValue();

		size_t vertexCount = pvertex.getCount();
		size_t normalCount = pnormal.getCount();

		for(size_t s = 0;s<sourceCount;++s)
		{
			domSourceRef pSource =  pMesh->getSource_array().get(s);
		    xString name = 	pSource->getName();
			xString element = pSource->getTypeName();
			domList_of_floats pPostion  = pSource->getFloat_array()->getValue();
		}
	//	pMesh->get
		size_t numTriangleGroups = (size_t)pMesh->getTriangles_array().getCount();
		for (size_t i=0; i< numTriangleGroups; i++)
		{
			
			domTrianglesRef pTriangles =  pMesh->getTriangles_array().get(i);
			size_t TriangleCount =   pTriangles->getCount();
			domList_of_uints& indexArray=  pTriangles->getP()->getValue();

			size_t tem =indexArray.getCount()/TriangleCount;

			int cc=0;++cc;
			


		}

		return true;
	}


}
